- [Instructor] Some loops let you repeat actions
a set number of times, such as the number of values
in a vector.
Sometimes, you'll either face random or unknown inputs
and need the loop to stop when limit is reached or before.
In this movie, I will show you how to use a while loop
to prevent MATLAB from exceeding a value.
I am in a blank command window,
but what I need to do is create a script.
So on the home tab, I will click New Script.
Or I could've also pressed control N.
The scenario that I have in mind
is one where I have a container
and I can load it up to a certain weight,
but I absolutely cannot go over.
That means that I need to check the weight as I go,
and refuse an item that would put the box
over its weight limit.
I will start by defining a variable called totalweight
and assign it the value of zero
and I'll follow that with a semicolon
so I don't get any output.
Then nextitem also equals zero.
I'm initializing these variables to zero.
They will change later.
Then enter.
Now I'm going to ask the user to input the maximum weight
of the container.
So I'll say wtlim, which in my mind, means weight limit
equals input, then left parenthesis, single quote,
Max weight followed by a colon and a space
then a backslash n for a new line character,
then a single quote to end the string,
and a semicolon so that I don't
get the input echoed, then enter.
Okay, now I can create what's called a while loop.
A while loop works as the name implies by operating
while a certain condition is true.
When that condition is no longer true, the loop stops.
So I'll start by typing the keyword while.
I want to check to make sure that the total weight
plus the weight of the next item is less than
or equal to the weight limit.
If it isn't, then I don't want the next item to be added
to the box.
So I'll type totalweight which starts at zero,
and then plus the weight of the nextitem,
which again is currently zero.
That total must be less than or equal
to the weight limit, wtlim,
and that's what we got from the user.
Enter.
If that condition is true,
that totalweight plus the nextitem is less than
or equal to the weight limit, then we can add
the weight of the next item.
So we have totalweight equals totalweight
plus the weight of the nextitem.
And I will suppress output with a semicolon, then enter.
Now we can generate randomly the weight of the next item.
So I'll say that's in the range from one to five.
So nextitem equals randi, random integers,
followed by a left parenthesis and then the range,
and I'll do this in square brackets, one comma five,
square brackets closed, and a right parenthesis
to close out the randi function arguments,
and a semicolon to end that statement without any output.
Enter.
Now I can end, and that is attached automatically
or organized to go with the while.
Now I can print the result,
and I want to print it as a decimal.
So I'll do fprintf, left parenthesis, single quote,
Total weight is, then a space, and then %d,
which provides a decimal number grams, period.
Then a space, then the new line character
which is backslash N.
Then a single quote to end the string, a comma,
and the variable that will provide the value
for the %d, or decimal number placeholder,
as totalweight, and right parenthesis.
That is the end of my procedure,
the end of my script,
so I'll press control, S, and I'll save it as tw.
That'll save me some typing when I go to run it.
So everything is good.
Press enter and there we are.
Okay, I will go ahead and close the editor window
and now if I type tw, and press enter,
I am prompted for the maximum weight.
I'll put in 21 and enter, and I see that the totalweight
is 20 grams.
So we got up to 20 and the next item would've taken
the box over 21 so we stopped.
I'll do tw again.
This time, let's say the maximum weight is 25, enter.
Totalweight is 21, so we didn't quite get there,
and now I'll do tw again one last time,
and let's say the maximum is 37, enter,
and we get all the way up to 33.
So you see a while loop is extremely useful
when you have a condition that cannot be exceeded.